package com.ttai.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerSetMiYaoAgainComPonet;
import com.ttai.dagger.module.activity.SetMiYaoAgainPresenterModule;
import com.ttai.presenter.activity.HomePresrenter;
import com.ttai.presenter.activity.InsertTipAcountPresenter;
import com.ttai.presenter.activity.SetMiYaoAgainPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.AesAndroid;
import com.ttai.untils.Constant;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetMiyaoAgain extends BaseActivity {
    @Bind(R.id.ed_passwordtipagain)
    EditText edPasswordtipagain;
    @Bind(R.id.et_keyagain)
    EditText etKeyagain;
    @Bind(R.id.tb_keyagain_visibility)
    ToggleButton tbKeyagainVisibility;
    @Bind(R.id.btn_jiamiagain)
    Button btnJiamiagain;

    @Inject
    SetMiYaoAgainPresenter setMiYaoAgainPresenter;
//    InsertTipAcountPresenter insertTipAcountPresenter;


    public String phonenumber;
    private String miYao;
    private String acountId;
    private String acountPassword;
    public String jiamiAcountId;
    public String jiamiAcountPassword;
    public String acountTip;
    public String value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setmiyaoagain);
        ButterKnife.bind(this);
        setComponet();
        Intent intent = getIntent();
        acountId = intent.getStringExtra("acountId");
        acountPassword = intent.getStringExtra("acountPassword");
        value = intent.getStringExtra("value");
//        insertTipAcountPresenter = new InsertTipAcountPresenter();
//        insertTipAcountPresenter.storeTipAcount(Constant.getToken());

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        tbKeyagainVisibility.setOnCheckedChangeListener(new ToggleButtonClick());

//        if (HomePresrenter.myPhone==null || HomePresrenter.myPhone.isEmpty()){
//            if(Register.phonenumber==null || Register.phonenumber.isEmpty()){
//                phonenumber = EnterPage.enterphonenumber;
//            }else{
//                phonenumber =Register.phonenumber;
//            }
//        }else {
//            phonenumber =HomePresrenter.myPhone;
//        }
        acountTip = edPasswordtipagain.getText().toString();

        /**
         * 检验miyaoTip是否存在，若存在则直接赋值
         */
//        value = insertTipAcountPresenter.getTip();

        if(value !=null && value.isEmpty()==false){
            edPasswordtipagain.setText(value);
            edPasswordtipagain.setInputType(InputType.TYPE_NULL);
        }
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void setComponet() {
        DaggerSetMiYaoAgainComPonet.builder()
                .setMiYaoAgainPresenterModule(new SetMiYaoAgainPresenterModule(this))
                .build()
                .in(this);
    }

    @OnClick(R.id.btn_jiamiagain)
    public void onViewClicked() {
        miYao = etKeyagain.getText().toString();
        if (miYao == null || miYao.isEmpty()) {
            Toast.makeText(SetMiyaoAgain.this, "加密失败,密钥不能为空", Toast.LENGTH_SHORT).show();
        } else {
            /**
             * SHA256密钥加密
             */

            String sha256Password = Constant.shaEncrypt(miYao);
            String token = Constant.getToken();
            Log.i("4", "onViewClicked: "+token+" miyao: "+miYao+" sha: "+sha256Password);
            setMiYaoAgainPresenter.verifyKey(token, sha256Password);


            if (miYao == null || miYao.isEmpty()) {
                Toast.makeText(SetMiyaoAgain.this, "加密失败,提示或秘钥不能为空", Toast.LENGTH_SHORT).show();
            }

            jiamiAcountId = Constant.string2Unicode(AesAndroid.encrypt(miYao, acountId));
            jiamiAcountPassword = Constant.string2Unicode(AesAndroid.encrypt(miYao, acountPassword));



        }
    }

        private class ToggleButtonClick implements CompoundButton.OnCheckedChangeListener {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //5、判断事件源的选中状态
                if (isChecked){

                    //显示密码
                    //etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    etKeyagain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    // 隐藏密码
                    //etPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    etKeyagain.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }
                //6、每次显示或者关闭时，密码显示编辑的线不统一在最后，下面是为了统一
                etKeyagain.setSelection(etKeyagain.length());
            }
        }
}
